package com.atguigu.orderservice.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.orderservice.entity.Order;
import com.atguigu.orderservice.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author thomasyang
 * @since 2023-12-22
 */
@CrossOrigin
@RestController
@RequestMapping("/eduorder/order")
public class OrderController {


    @Autowired
    private OrderService orderService;


    @ApiOperation(value = "根据课程id生成订单")
    @PostMapping("createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId, HttpServletRequest request){

        String OrderId = orderService.createOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));

        return R.ok().data("orderId" ,OrderId);
    }

    @ApiOperation(value = "根据订单id获取订单信息")
    @GetMapping("getOrderById/{orderId}")
    public R getOrderById(@PathVariable String orderId){

        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderId);
        Order order = orderService.getOne(wrapper);

        return R.ok().data(order);

    }

}

